<?php
// +----------------------------------------------------------------------
// | ThinkBLOG (Build on ThinkPHP)
// +----------------------------------------------------------------------
// | @link ( http://www.yurnero.net )
// +----------------------------------------------------------------------
// | @licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | @author nicholas <nicholasinlove@126.com>
// +----------------------------------------------------------------------
// | $Id: Page.class.php 120 2012-04-16 02:30:46Z nicholasinlove1986@gmail.com $
// +----------------------------------------------------------------------


class Page {
	// 分页栏每页显示的页数
	public $rollPage = 5;
	// 页数跳转时要带的参数
	public $parameter;
	// 默认列表每页显示行数
	public $listRows = 20;
	// 起始行数
	public $firstRow;
	// 分页总页面数
	protected $totalPages;
	// 总行数
	protected $totalRows;
	// 当前页数
	protected $nowPage;
	// 分页的栏的总页数
	protected $coolPages;
	// 最后一页是否显示总页数
	private $lastSuffix = true;
	// 分页显示定制
	protected $config = array ('header' => '条记录', 'prev' => '上一页', 'next' => '下一页', 'first' => '第一页', 'last' => '最后一页', 'theme' => ' %totalRow% %header% %nowPage%/%totalPage% 页 %upPage% %downPage% %first%  %prePage%  %linkPage%  %nextPage% %end%' );
	// 默认分页变量名
	protected $varPage;
	
	/**
     +----------------------------------------------------------
	 * 架构函数
     +----------------------------------------------------------
	 * @access public
     +----------------------------------------------------------
	 * @param array $totalRows  总的记录数
	 * @param array $listRows  每页显示记录数
	 * @param array $parameter  分页跳转的参数
     +----------------------------------------------------------
	 */
	public function __construct($totalRows, $listRows = '', $parameter = array()) {
		$this->totalRows = $totalRows;
		$this->parameter = $parameter;
		$this->varPage = C ( 'VAR_PAGE' ) ? C ( 'VAR_PAGE' ) : 'p';
		if (! empty ( $listRows )) {
			$this->listRows = intval ( $listRows );
		}
		$this->totalPages = ceil ( $this->totalRows / $this->listRows );
		$this->coolPages = ceil ( $this->totalPages / $this->rollPage );
		$this->nowPage = (! empty ( $_GET [$this->varPage] ) && is_numeric ( $_GET [$this->varPage] )) ? intval ( $_GET [$this->varPage] ) : 1;
		if (! empty ( $this->totalPages ) && $this->nowPage > $this->totalPages) {
			$this->nowPage = $this->totalPages;
		}
		$this->firstRow = $this->listRows * ($this->nowPage - 1);
	}
	
	public function setConfig($name, $value) {
		if (isset ( $this->config [$name] )) {
			$this->config [$name] = $value;
		}
	}
	
	/**
     +----------------------------------------------------------
	 * 分页显示输出
     +----------------------------------------------------------
	 * @access public
     +----------------------------------------------------------
	 */
	public function show() {
		if (0 == $this->totalRows)
			return ''; // 没有记录不显示
		$p = $this->varPage;
		$nowCoolPage = $this->rollPage / 2;
		$nowCoolPageCeil = ceil ( $nowCoolPage );
		if ($this->lastSuffix)
			$this->config ['last'] .= $this->totalPages;
		
		$upRow = $this->nowPage - 1;
		$downRow = $this->nowPage + 1;
		
		if ($upRow > 0) {
			$this->parameter [$p] = $upRow;
			$upPage = '<a href="' . U ( GROUP_NAME . '/' . MODULE_NAME . '/' . ACTION_NAME, $this->parameter ) . '">' . $this->config ['prev'] . '</a>';
		} else {
			$upPage = '';
		}
		
		if ($downRow <= $this->totalPages) {
			$this->parameter [$p] = $downRow;
			$downPage = '<a href="' . U ( GROUP_NAME . '/' . MODULE_NAME . '/' . ACTION_NAME, $this->parameter ) . '">' . $this->config ['next'] . '</a>';
		} else {
			$downPage = '';
		}
		
		if ($this->totalPages <= $this->rollPage || ($this->nowPage - $nowCoolPage) < 1) {
			$theFirst = '';
			$prePage = '';
		} else {
			$this->parameter [$p] = $this->nowPage - $this->rollPage;
			$prePage = '<a href="' . U ( GROUP_NAME . '/' . MODULE_NAME . '/' . ACTION_NAME, $this->parameter ) . '">上' . $this->rollPage . '页</a>';
			$this->parameter [$p] = 1;
			$theFirst = '<a href="' . U ( GROUP_NAME . '/' . MODULE_NAME . '/' . ACTION_NAME, $this->parameter ) . '">' . $this->config ['first'] . '</a>';
		}
		if ($this->totalPages <= $this->rollPage || ($this->nowPage + $nowCoolPage) >= $this->totalPages) {
			$nextPage = '';
			$theEnd = '';
		} else {
			$this->parameter [$p] = $this->nowPage + $this->rollPage;
			$nextPage = '<a href="' . U ( GROUP_NAME . '/' . MODULE_NAME . '/' . ACTION_NAME, $this->parameter ) . '">下' . $this->rollPage . '页</a>';
			$this->parameter [$p] = $this->totalPages;
			$theEnd = '<a href="' . U ( GROUP_NAME . '/' . MODULE_NAME . '/' . ACTION_NAME, $this->parameter ) . '">' . $this->config ['last'] . '</a>';
		}
		
		$linkPage = '';
		for($i = 1; $i <= $this->rollPage; $i ++) {
			if (($this->nowPage - $nowCoolPage) <= 0) {
				$page = $i;
			} elseif (($this->nowPage + $nowCoolPage - 1) >= $this->totalPages) {
				$page = $this->totalPages - $this->rollPage + $i;
			} else {
				$page = $this->nowPage - $nowCoolPageCeil + $i;
			}
			if ($page > 0 && $page != $this->nowPage) {
				if ($page <= $this->totalPages) {
					$this->parameter [$p] = $page;
					$linkPage .= '<a href="' . U ( GROUP_NAME . '/' . MODULE_NAME . '/' . ACTION_NAME, $this->parameter ) . '">' . $page . '</a>';
				} else {
					break;
				}
			} else {
				if ($page > 0 && $this->totalPages != 1) {
					//$linkPage .= '<span class="current">' . $page . '</span>';
					$linkPage .= "<span class='paging-input'><input class='current-page' type='text' name='paged' value='" . $page . "' size='1' /><span>";
				}
			}
		}
		
		$pageStr = str_replace ( array ('%header%', '%nowPage%', '%totalRow%', '%totalPage%', '%upPage%', '%downPage%', '%first%', '%prePage%', '%linkPage%', '%nextPage%', '%end%' ), array ($this->config ['header'], $this->nowPage, $this->totalRows, $this->totalPages, $upPage, $downPage, $theFirst, $prePage, $linkPage, $nextPage, $theEnd ), $this->config ['theme'] );
		return $pageStr;
	}

}